package com.example.novelweb.utils;

import cn.hutool.core.lang.UUID;
import com.example.novelweb.constant.UserConstant;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtils {


  /**
   * @return <timestamp>_<UUID>_<userId | other_unique_operator_id>
   */
  public static String uniqueFileNameGenerate(Long operatorId) {
    return System.currentTimeMillis()+"_"+ UUID.randomUUID().toString(true)+"_"+operatorId;
  }

  public static String uniqueFileNameGenerate(String originalFileName) {
    return System.currentTimeMillis()+"_"+ UUID.randomUUID().toString(true)+"_"+originalFileName;
  }

  public static boolean isImgFileExist(String outsidePath) {
    Path imgPath = Paths.get(UserConstant.IMG_RES_PATH).toAbsolutePath().normalize();
    String root = UserConstant.SERVER_URL_PREFIX+"images/";
    String name = outsidePath.substring(root.length());

    Path filePath = imgPath.resolve(name);
    return Files.exists(filePath);
  }
}
